package com.chen.shopping_user_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.shopping_common.pojo.ShoppingUser;
import com.chen.shopping_common.result.BusException;
import com.chen.shopping_common.result.CodeEnum;
import com.chen.shopping_common.service.ShoppingUserService;
import com.chen.shopping_common.utils.Md5Utils;
import com.chen.shopping_user_service.mapper.ShoppingUserMapper;
import com.chen.shopping_user_service.utils.JwtUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@DubboService
public class ShoppingUserServiceImpl implements ShoppingUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ShoppingUserMapper shoppingUserMapper;

    //将注册的手机号和验证码保存到redis中
    @Override
    public void saveRegisterCheckNode(String phone, String checkCode) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //键值对方式存储手机号和验证码，设置过期时间300秒
        valueOperations.set("registryCode:"+phone,checkCode,300, TimeUnit.SECONDS);
    }

    //验证验证码是否正确
    @Override
    public void registerCheckCode(String phone, String checkCode) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object code = valueOperations.get("registryCode:" + phone);
        if (!code.equals(checkCode)){
            throw new BusException(CodeEnum.REDISTRY_CODE_ERROR);
        }

    }

    @Override
    public void register(ShoppingUser shoppingUser) {
        //验证手机号是否存在
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",shoppingUser.getPhone());
        List<ShoppingUser> list = shoppingUserMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0){
            throw  new BusException(CodeEnum.REGISTRY_PHONE_EXIST);
        }
        //验证用户名是否存在
        QueryWrapper<ShoppingUser> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("username",shoppingUser.getUsername());
        List<ShoppingUser> list1 = shoppingUserMapper.selectList(queryWrapper1);
        if (list1 != null && list1.size() > 0){
            throw new BusException(CodeEnum.REGISTRY_USERNAME_EXIST);
        }
        //新增用户
        shoppingUser.setStatus("Y");
        shoppingUser.setPassword(Md5Utils.encode(shoppingUser.getPassword()));
        shoppingUserMapper.insert(shoppingUser);
    }

    @Override
    public String loginPassword(String username, String password) {
        //通过用户名查询用户
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        if (shoppingUser == null){
            throw new BusException(CodeEnum.LOGIN_USERNAME_PASSWORD_ERROR);
        }
        //获取密码并进行密码比对
        String password1 = shoppingUser.getPassword();
        //verify方法：前面传入明文，后面传入密文
        if (!Md5Utils.verify(password,password1)){
            throw new BusException(CodeEnum.LOGIN_USERNAME_PASSWORD_ERROR);
        }
        //返回令牌
        String sign = JwtUtils.sign(shoppingUser.getId(), shoppingUser.getUsername());
        return sign;
    }

    @Override
    public void saveLoginCheckCode(String phone, String checkCode) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //键值对方式存储手机号和验证码，设置过期时间300秒
        valueOperations.set("loginCode:"+phone,checkCode,300, TimeUnit.SECONDS);
    }

    @Override
    public String loginCheckCode(String phone, String checkCode) {
        //获取redis中的验证码
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object code = valueOperations.get("loginCode:" + phone);
        if (!code.equals(checkCode)){
            throw new BusException(CodeEnum.LOGIN_CHECKCODE_ERROR);
        }
        //通过手机号查询用户
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        //返回令牌
        String sign = JwtUtils.sign(shoppingUser.getId(), shoppingUser.getUsername());
        return sign;
    }

    @Override
    public void checkPhone(String phone) {
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        if (shoppingUser == null){
            throw new BusException(CodeEnum.LOGIN_NOPHONE_ERROR);
        }
        if (!shoppingUser.getStatus().equals("Y")){
            throw new BusException(CodeEnum.LOGIN_USER_STATUS_ERROR);
        }
    }

    //通过令牌获取用户名
    @Override
    public String getName(String token) {
        Map<String, Object> verify = JwtUtils.verify(token);
        String username =(String) verify.get("username");
        return username;
    }

    //通过令牌获取用户对象
    @Override
    public ShoppingUser getLoginUser(String token) {
        Map<String, Object> verify = JwtUtils.verify(token);
        Long userId = (Long) verify.get("userId");
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",userId);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        return shoppingUser;
    }
}
